//
//  CDPwdLoginStep.swift
//  casdon_swiftUI
//
//  Created by ww on 2025/4/20.
//

import Combine


struct CDPwdLoginStep: LoginStep {
    struct InputData {
        let userName: String
        let password: String
    }

    func execute(_ input: InputData) -> AnyPublisher<AuthAPI.LoginPwd.Response, Error> {
        Future { promise in
            let api = AuthAPI.LoginPwd(body: .init(userName: input.userName, password: input.password))
            NetworkManager.shared.send(api, completion: { result in
                switch result {
                    case .success(let response):
                        if let response = response.data
                        {
                            Logger.log(.info, message: "密码登陆成功: \(response)",tag: .login)
                            promise(.success(response))
                        }
                        else{
                            promise(.failure(NSError(domain: "账号密码登录", code: response.code as! Int, userInfo: [NSLocalizedDescriptionKey: response.msg ?? "无错误描述"])))
                        }
                    case .failure(let error):
                        promise(.failure(error))
                    }
            })
        }
        .eraseToAnyPublisher()
    }
}
